<?php

/**
 * Desc
 * @description Holp You Do Good But Not Evil
 * @copyright   Copyright 2014-2015 <ycchen@iwshop.cn>
 * @license     LGPL (http://www.gnu.org/licenses/lgpl.html)
 * @author      Chenyong Cai <ycchen@iwshop.cn>
 * @package     Wshop
 * @link        http://www.iwshop.cn
 */
class CreditExchange extends Model {

    public function getList() {
        return $this->Dao->select("ex.*, pd.product_name")->from(TABLE_CREDIT_EXCHANGE)->alias('ex')
                        ->leftJoin(TABLE_PRODUCTS)->alias('pd')
                        ->on("pd.product_id = ex.product_id")->exec();
    }

    /**
     * 
     * @param type $id
     * @param type $credit
     */
    public function add($id, $credit) {
        return $this->Dao->insert(TABLE_CREDIT_EXCHANGE, 'product_id, product_credits')->values(array($id, $credit))->exec();
    }

    /**
     * 
     * @param type $id
     */
    public function del($id) {
        if ($id > 0) {
            return $this->Dao->delete()->from(TABLE_CREDIT_EXCHANGE)->where('product_id = ' . $id)->exec();
        }
        return false;
    }

    /**
     * 
     * @param type $id
     * @param type $credit
     */
    public function modi($id, $credit) {
        return $this->Dao->update(TABLE_CREDIT_EXCHANGE)->set(array('product_credits' => $credit))->where('product_id = ' . $id)->exec();
    }

}
